const http = require("http");
const fs = require("fs");
const path = require("path");

http.createServer((req, res)=>{
    var urlStr = req.url;
    var filePath = path.join(__dirname, "/test.html");
    var imgPath = path.join(__dirname, "/icon.png");
    /* 当请求的资源路径是 ‘/’ 时，响应网页文件到浏览器 */
    if(urlStr =="/") { //在powershell中输入node server.js 会返回"/
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, {"Content-Type": "text/html;charset=utf8"}) //成功找到的状态码是200
        res.write(fileContent);
        res.end();
        
    }
    else if(urlStr == "/icon.png"){
        var imgContent = fs.readFileSync(imgPath);
        res.writeHead(200, {"Content-Type": "image/png"});
        res.write(imgContent);
        res.end();
    }
}).listen(8082);

console.log("server is listening 8082");